home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap15 / Apollo11 / Apollo11.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  6.5 KB  |  176 lines

  1. /*----------------------------------------------
  2.    APOLLO11.C -- Program for screen captures
  3.                  (c) Charles Petzold, 1998
  4.   ----------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "dibfile.h"
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. TCHAR szAppName[] = TEXT ("Apollo11") ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15. {
  16.      HWND     hwnd ;
  17.      MSG      msg ;
  18.      WNDCLASS wndclass ;
  19.  
  20.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  21.      wndclass.lpfnWndProc   = WndProc ;
  22.      wndclass.cbClsExtra    = 0 ;
  23.      wndclass.cbWndExtra    = 0 ;
  24.      wndclass.hInstance     = hInstance ;
  25.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  26.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  27.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  28.      wndclass.lpszMenuName  = NULL ;
  29.      wndclass.lpszClassName = szAppName ;
  30.  
  31.      if (!RegisterClass (&wndclass))
  32.      {
  33.           MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
  34.                       szAppName, MB_ICONERROR) ;
  35.           return 0 ;
  36.      }
  37.  
  38.      hwnd = CreateWindow (szAppName, TEXT ("Apollo 11"),
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT, 
  42.                           NULL, NULL, hInstance, NULL) ;
  43.  
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.  
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.      {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.      }
  52.      return msg.wParam ;
  53. }
  54.  
  55. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  56. {
  57.      static BITMAPFILEHEADER * pbmfh [2] ;
  58.      static BITMAPINFO       * pbmi  [2] ;
  59.      static BYTE             * pBits [2] ;
  60.      static int                cxClient, cyClient, cxDib [2], cyDib [2] ;
  61.      HDC                       hdc ;
  62.      PAINTSTRUCT               ps ;
  63.  
  64.      switch (message)
  65.      {
  66.      case WM_CREATE:
  67.           pbmfh[0] = DibLoadImage (TEXT ("Apollo11.bmp")) ;
  68.           pbmfh[1] = DibLoadImage (TEXT ("ApolloTD.bmp")) ;
  69.  
  70.           if (pbmfh[0] == NULL || pbmfh[1] == NULL)
  71.           {
  72.                MessageBox (hwnd, TEXT ("Cannot load DIB file"), 
  73.                                 szAppName, 0) ;
  74.                return 0 ;
  75.           }
  76.                // Get pointers to the info structure & the bits
  77.  
  78.           pbmi  [0] = (BITMAPINFO *) (pbmfh[0] + 1) ;
  79.           pbmi  [1] = (BITMAPINFO *) (pbmfh[1] + 1) ;
  80.  
  81.           pBits [0] = (BYTE *) pbmfh[0] + pbmfh[0]->bfOffBits ;
  82.           pBits [1] = (BYTE *) pbmfh[1] + pbmfh[1]->bfOffBits ;
  83.  
  84.                // Get the DIB width and height (assume BITMAPINFOHEADER)
  85.                // Note that cyDib is the absolute value of the header value!!!
  86.  
  87.           cxDib [0] =      pbmi[0]->bmiHeader.biWidth ;
  88.           cxDib [1] =      pbmi[1]->bmiHeader.biWidth ;
  89.  
  90.           cyDib [0] = abs (pbmi[0]->bmiHeader.biHeight) ;
  91.           cyDib [1] = abs (pbmi[1]->bmiHeader.biHeight) ;
  92.           return 0 ;
  93.  
  94.      case WM_SIZE:
  95.           cxClient = LOWORD (lParam) ;
  96.           cyClient = HIWORD (lParam) ;
  97.           return 0 ;
  98.          
  99.      case WM_PAINT:
  100.           hdc = BeginPaint (hwnd, &ps) ;
  101.  
  102.                // Bottom-up DIB full size
  103.  
  104.           SetDIBitsToDevice (hdc, 
  105.                              0,                   // xDst
  106.                              cyClient / 4,        // yDst
  107.                              cxDib[0],            // cxSrc
  108.                              cyDib[0],            // cySrc
  109.                              0,                   // xSrc
  110.                              0,                   // ySrc
  111.                              0,                   // first scan line
  112.                              cyDib[0],            // number of scan lines
  113.                              pBits[0], 
  114.                              pbmi[0], 
  115.                              DIB_RGB_COLORS) ;
  116.  
  117.                // Bottom-up DIB partial
  118.  
  119.           SetDIBitsToDevice (hdc, 
  120.                              240,                 // xDst
  121.                              cyClient / 4,        // yDst
  122.                              80,                  // cxSrc
  123.                              166,                 // cySrc
  124.                              80,                  // xSrc
  125.                              60,                  // ySrc
  126.                              0,                   // first scan line
  127.                              cyDib[0],            // number of scan lines
  128.                              pBits[0], 
  129.                              pbmi[0], 
  130.                              DIB_RGB_COLORS) ;
  131.  
  132.                // Top-down DIB full size
  133.  
  134.           SetDIBitsToDevice (hdc, 
  135.                              340,                 // xDst
  136.                              cyClient / 4,        // yDst
  137.                              cxDib[0],            // cxSrc
  138.                              cyDib[0],            // cySrc
  139.                              0,                   // xSrc
  140.                              0,                   // ySrc
  141.                              0,                   // first scan line
  142.                              cyDib[0],            // number of scan lines
  143.                              pBits[0], 
  144.                              pbmi[0], 
  145.                              DIB_RGB_COLORS) ;
  146.  
  147.                // Top-down DIB partial
  148.  
  149.           SetDIBitsToDevice (hdc, 
  150.                              580,                 // xDst
  151.                              cyClient / 4,        // yDst
  152.                              80,                  // cxSrc
  153.                              166,                 // cySrc
  154.                              80,                  // xSrc
  155.                              60,                  // ySrc
  156.                              0,                   // first scan line
  157.                              cyDib[1],            // number of scan lines
  158.                              pBits[1], 
  159.                              pbmi[1], 
  160.                              DIB_RGB_COLORS) ;
  161.  
  162.           EndPaint (hwnd, &ps) ;
  163.           return 0 ;
  164.           
  165.      case WM_DESTROY:
  166.           if (pbmfh[0])
  167.                free (pbmfh[0]) ;
  168.  
  169.           if (pbmfh[1])
  170.                free (pbmfh[1]) ;
  171.  
  172.           PostQuitMessage (0) ;
  173.           return 0 ;
  174.      }
  175.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  176. }